###### Appendix: Figure A3
###### Mean Punishment Thresholds, Ownership, and Ladder Position by Treatment Condition
gc(); rm(list = ls()); set.seed(12345)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Note: if you are not using R Studio this command will not work, set WD to source file location manually

############### SCRIPT SUMMARY #####################

packages <- c("dplyr", "ggplot2", "patchwork")
lapply(packages, require, character.only = T)
source("functions.R")

### Load Datasets

dfMerge <- read.csv("data/cleaned/uganda_jun17_cleaned.csv", header=T, stringsAsFactors =F)
dfMerge <- dfMerge[!dfMerge$game_id_exp %in% c("Basic Aid", "Aid Ownership"), ]

########################################## MISC PREP AND VARGEN ######################################

## Re-level treatment variable according to which one you want
dfMerge$treatment_pooled <- relevel(as.factor(dfMerge$treatment_pooled), ref = "Weak Hidden VAT") ## Set reference category for all future data
dfExp <- dfMerge
dfExp <- dfExp[dfExp$role == "C" & dfExp$round != 1 & 
                 !is.na(dfExp$treatment_pooled), ] # First round is a practice round

################## VISUALIZATION #######################

## Define some common variables

barWidth <- 1.1 # this is lwd argument
ptSize <- 2.5
posDodge <- 1/3
figHeight <- 5
figWidth <- 8.5


################ PANEL A: THRESHOLD AS DV

dfUsing <- dfExp
alpha <- 0.05

rawMeans <-  Rmisc::summarySE(dfUsing, measurevar = "sbj_thresh", 
                              groupvars = c("treatment_pooled"))
rawMeans <- subset(rawMeans, !is.na(treatment_pooled))
rawMeans$treatment_pooled <- gsub("Weak Hidden VAT", "Visible VAT" ,rawMeans$treatment_pooled)
rawMeans$treatment_pooled <- gsub("Strong Hidden VAT", "Hidden VAT", rawMeans$treatment_pooled)
rawMeans$conf.high_95 <- rawMeans$sbj_thresh + qnorm(alpha/2)*rawMeans$se
rawMeans$conf.low_95 <- rawMeans$sbj_thresh - qnorm(alpha/2)*rawMeans$se
rawMeans <- rawMeans %>% mutate(
  treatment_pooled = 
    factor(treatment_pooled, 
           levels=c("Windfall", "Hidden VAT", 
                    "Visible VAT", "Direct Tax")))

plotThresh <- ggplot(rawMeans, aes(x = treatment_pooled, y = sbj_thresh)) + 
  geom_linerange(aes(x = treatment_pooled, ymin = conf.low_95,
                     ymax = conf.high_95, size = I(10)),
                 lwd = barWidth, position = position_dodge(width = posDodge))  +
  geom_pointrange(aes(x = treatment_pooled, y = sbj_thresh, 
                      ymin =  conf.low_95, ymax = conf.high_95),
                  lwd = 0.8, position = position_dodge(width = posDodge),
                  shape = 21, fill = "WHITE", fatten = ptSize) + 
  ylab("Threshold (0-1000 shillings)") + ylim(475, 645) + 
  coord_flip() +  xlab("") + theme_gray() + 
  labs(title = "Main Outcome: \nPunishment Threshold")

plotThresh <- plotThresh + 
  theme_custom(axis_text_size = 8, 
               title_size = 10, 
               axis_title_size = 10, 
               title_position = 0) + 
  theme(legend.position = "none", plot.tag.position = "topleft", plot.margin = unit(c(0,.5,0,0), "cm"))  

################ PANEL B: OWNERSHIP AS DV

dfUsing <- dfExp[dfExp$round == 2, ]
alpha <- 0.05
depVar <- "strong_ownership"

rawMeans <-  Rmisc::summarySE(dfUsing, measurevar = depVar, 
                              groupvars = c("treatment_pooled"), na.rm = T)
rawMeans <- subset(rawMeans, !is.na(treatment_pooled))
rawMeans$treatment_pooled <- gsub("Weak Hidden VAT", "Visible VAT", 
                                  rawMeans$treatment_pooled)
rawMeans$treatment_pooled <- gsub("Strong Hidden VAT", "Hidden VAT", 
                                  rawMeans$treatment_pooled)
rawMeans$conf.high_95 <- rawMeans[, depVar] + qnorm(alpha/2)*rawMeans$se
rawMeans$conf.low_95 <- rawMeans[, depVar] - qnorm(alpha/2)*rawMeans$se
rawMeans <- rawMeans %>% mutate(
  treatment_pooled = 
    factor(treatment_pooled, 
           levels=c("Windfall", "Hidden VAT", 
                    "Visible VAT", "Direct Tax")))

plotOwn <- ggplot(rawMeans, aes(x = treatment_pooled, y = strong_ownership)) + 
  geom_linerange(aes(x = treatment_pooled, ymin = conf.low_95,
                     ymax = conf.high_95, size = I(10)),
                 lwd = barWidth, position = position_dodge(width = posDodge))  +
  geom_pointrange(aes(x = treatment_pooled, y = strong_ownership, 
                      ymin =  conf.low_95, ymax = conf.high_95),
                  lwd = 0.8, position = position_dodge(width = posDodge),
                  shape = 21, fill = "WHITE", fatten = ptSize) + 
  ylab("Ownership over Budget (0-10)") +
  coord_flip() +  xlab("") + theme_gray() + 
  ylim(5.4, 7.6) + 
  labs(title = "Mechanism 1: \nOwnership over Group Fund")

plotOwn <- plotOwn + 
  theme_custom(axis_text_size = 8, 
               title_size = 10, 
               axis_title_size = 10, 
               title_position = 0) + 
  theme(legend.position = "none", 
                        axis.text.y = element_blank(), 
                        axis.ticks.y = element_blank(), plot.margin = unit(c(0,.5,0,0), "cm"))


################ PANEL C: LADDER AS DV

dfUsing <- dfExp
alpha <- 0.05
depVar <- "l1"

rawMeans <-  Rmisc::summarySE(dfUsing, measurevar = depVar, 
                              groupvars = c("treatment_pooled"), na.rm = T)
rawMeans <- subset(rawMeans, !is.na(treatment_pooled))
rawMeans$treatment_pooled <- gsub("Weak Hidden VAT", "Visible VAT", 
                                  rawMeans$treatment_pooled)
rawMeans$treatment_pooled <- gsub("Strong Hidden VAT", "Hidden VAT", 
                                  rawMeans$treatment_pooled)
rawMeans$conf.high_95 <- rawMeans[, depVar] + qnorm(alpha/2)*rawMeans$se
rawMeans$conf.low_95 <- rawMeans[, depVar] - qnorm(alpha/2)*rawMeans$se
rawMeans <- rawMeans %>% mutate(
  treatment_pooled = 
    factor(treatment_pooled, 
           levels=c("Windfall", "Hidden VAT", 
                    "Visible VAT", "Direct Tax")))
rawMeans

plotLoss <- ggplot(rawMeans, aes(x = treatment_pooled, y = l1)) + 
  geom_linerange(aes(x = treatment_pooled, ymin = conf.low_95,
                     ymax = conf.high_95, size = I(10)),
                 lwd = barWidth, position = position_dodge(width = posDodge))  +
  geom_pointrange(aes(x = treatment_pooled, y = l1, 
                      ymin =  conf.low_95, ymax = conf.high_95),
                  lwd = 0.8, position = position_dodge(width = posDodge),
                  shape = 21, fill = "WHITE", fatten = ptSize) + 
  ylab("Ladder Position (0-20)") +
  ylim(6.0, 7.6) +
  coord_flip() +  xlab("") + theme_gray() + 
  labs(title = "Mechanism 2: \nPost Purchase Utility") 
  geom_hline(yintercept = 10, color = "red", linetype = 2) 

plotLoss <- plotLoss + 
  theme_custom(axis_text_size = 8, 
               title_size = 10, 
               axis_title_size = 10, 
               title_position = 0) + 
  theme(legend.position = "none", 
        axis.text.y = element_blank(), 
        axis.ticks.y = element_blank())



########## PUT THEM ALL TOGETHER

require(patchwork)

patchwork <- (plotThresh + plotOwn + plotLoss)
patchwork <- patchwork + plot_annotation(
tag_levels = 'A' 
)
print(patchwork)


pdf(file = "figures/appendix/appendix_fig_a3.pdf", 
    height = figHeight, width = figWidth)
print(patchwork)
dev.off()

save.image(file = "results/fig_a3.RData")




